import { AnyObject } from 'element-plus/lib/el-table/src/table.type';
import type { App } from 'vue';
import axiosHelper from './axiosHelper';

function configCollect() {
  let collect: AnyObject = {};
  const file = require.context('./config', true, /\.ts$/);
  file.keys().forEach(item => {
    const path = item.replace('./', './config/');
    const file: HttpFile = require(`${path}`).default;
    const arr = path.split('/');
    const fileName = arr[arr.length - 1].replace('.ts', '');
    collect[fileName] = {};
    for (const i in file) {
      const { method, url } = file[i];
      collect[fileName][i] = function(
          resetParams: AnyObject = {},
          params: AnyObject = {},
          headers?: AnyObject,
      ) {
        return axiosHelper[method || 'get'](url, resetParams, params, headers);
      };
    }
  });

  return collect;
}

function install(app: App): any {
  console.log(app);
  // console.log(app._context);
  const config = configCollect();
  app.config.globalProperties.$api = config;
}

export default {
  install,
};
